package cn.tedu;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtils {
    private static DruidDataSource dds;//提升作用域
    static {//
        //创建连池对象
        dds = new DruidDataSource();
        //设置数据库连接信息
        dds.setUrl("jdbc:mysql://localhost:3306/empdb?" +
                "characterEncoding=utf8&" +
                "serverTimezone=Asia/Shanghai");
        dds.setUsername("root");
        dds.setPassword("root");
        //设置初始连接数量
        dds.setInitialSize(3);
        //设置最大连接数量
        dds.setMaxActive(5);
    }
    //获取连接
    public static Connection getConn() throws SQLException {

        //从连接池中获取连接  异常抛出
        DruidPooledConnection conn = dds.getConnection();
        System.out.println(conn);
        return conn;
    }
}
